package com.atguigu.yygh.hosp.controller.api;

import com.atguigu.yygh.common.exception.YyghException;
import com.atguigu.yygh.common.result.Result;
import com.atguigu.yygh.common.result.ResultCodeEnum;
import com.atguigu.yygh.comon.helper.HttpRequestHelper;
import com.atguigu.yygh.comon.utils.MD5;
import com.atguigu.yygh.hosp.service.DeparmentService;
import com.atguigu.yygh.hosp.service.HospitalService;
import com.atguigu.yygh.hosp.service.HospitalSetService;
import com.atguigu.yygh.hosp.service.ScheduleService;
import com.atguigu.yygh.model.hosp.Department;
import com.atguigu.yygh.model.hosp.Hospital;
import com.atguigu.yygh.model.hosp.Schedule;
import com.atguigu.yygh.vo.hosp.DepartmentQueryVo;

import com.atguigu.yygh.vo.hosp.ScheduleOrderVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.util.StringUtils;
import org.springframework.web.HttpRequestHandler;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@RestController
@RequestMapping("/api/hosp")
public class ApiController {

   @Autowired
   private DeparmentService  deparmentService;


     @Autowired
    private HospitalService hospitalService;

     @Autowired
     private HospitalSetService  hospitalSetService;


     @Autowired
     private ScheduleService scheduleService;


     //查询医院
    @PostMapping("hospital/show")
    public Result getHospital(HttpServletRequest  request){

        Map<String, String[]> parameterMap = request.getParameterMap();
        Map<String, Object> param = HttpRequestHelper.switchMap(parameterMap);

         //获取传递过来医院编号
        String  hoscode = (String)param.get("hoscode");
                     String  hospSign   = (String)param.get("sign");

        String signKey = hospitalSetService.getSignKey(hoscode);

        String encrypt = MD5.encrypt(signKey);

        if(!hospSign.equals(encrypt)){
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }



      Hospital  hosital  =     hospitalService.getByHoscode(hoscode);

        return Result.ok(hosital);


    }






  //上传医院接口
    @PostMapping("saveHospital")
    public Result saveHosp(HttpServletRequest request){

    //获取传递过来医院信息
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);
        String  sign =(String ) paramMap.get("sign");
                     String code  =(String)paramMap.get("hoscode");
               String signKey     = hospitalSetService.getSignKey(code);
        String signKeyMd5 = MD5.encrypt(signKey);

        String logoData =(String) paramMap.get("logoData");
        logoData = logoData.replaceAll(" ", "+");
        paramMap.put("logoData",logoData);

        if(signKeyMd5.equals(sign)){
            hospitalService.save(paramMap);

            return  Result.ok();

        }else{


            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }

        //调用service方法


    }

   //上传科室接口
    @PostMapping("saveDepartment")
    public  Result  saveDepartment(HttpServletRequest  request){
//获取传递过来医院信息
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> param = HttpRequestHelper.switchMap(requestMap);

        //获取传递过来医院编号
        String  hoscode = (String)param.get("hoscode");
        String  hospSign   = (String)param.get("sign");

        String signKey = hospitalSetService.getSignKey(hoscode);

        String encrypt = MD5.encrypt(signKey);

        if(!hospSign.equals(encrypt)){
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }


        Boolean  flag   = deparmentService.save(param);


        return Result.ok();
    }


  //查询科室接口
    @PostMapping("department/list")
    public  Result  findDepartment(HttpServletRequest  request){


        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> param = HttpRequestHelper.switchMap(requestMap);


  int  page= StringUtils.isEmpty(param.get("page"))?1:Integer.parseInt((String) param.get("page"));
  int limit=StringUtils.isEmpty(param.get("limit"))?1:Integer.parseInt((String) param.get("limit"));
        // 签名校验
        String hoscode = (String)param.get("hoscode");
        String  hospSign   = (String)param.get("sign");

        String signKey = hospitalSetService.getSignKey(hoscode);

        String encrypt = MD5.encrypt(signKey);

        if(!hospSign.equals(encrypt)){
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }

        DepartmentQueryVo  departmentQueryVo=new DepartmentQueryVo();

        departmentQueryVo.setHoscode(hoscode);

        //调用service方法

        Page<Department> pageModel  =deparmentService.findPageDeparment(page,limit,departmentQueryVo);

        return  Result.ok(pageModel);
    }

   // 删除科室接口
    @PostMapping("department/remove")
    public  Result  departmentRemove(HttpServletRequest  request){

        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> param = HttpRequestHelper.switchMap(requestMap);
        String hoscode = (String)param.get("hoscode");
        String  hospSign   = (String)param.get("sign");
                  String depcode    = (String) param.get("depcode");
        String signKey = hospitalSetService.getSignKey(hoscode);

        String encrypt = MD5.encrypt(signKey);

        if(!hospSign.equals(encrypt)){
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }

        deparmentService.remove(hoscode,depcode);
        return  Result.ok();
    }


    //上传排班
    @PostMapping("saveSchedule")
    public  Result  saveSchedule(HttpServletRequest  request){

        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> param = HttpRequestHelper.switchMap(requestMap);
        String hoscode = (String)param.get("hoscode");
        String  hospSign   = (String)param.get("sign");
        String depcode    = (String) param.get("depcode");
        String signKey = hospitalSetService.getSignKey(hoscode);

        String encrypt = MD5.encrypt(signKey);

        if(!hospSign.equals(encrypt)){
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }



     scheduleService.save(param);

        return  Result.ok();

    }
// 查询排班

  @PostMapping("schedule/list")
    public  Result  findSchedule(HttpServletRequest request){

      Map<String, String[]> requestMap = request.getParameterMap();
      Map<String, Object> param = HttpRequestHelper.switchMap(requestMap);


      int  page= StringUtils.isEmpty(param.get("page"))?1:Integer.parseInt((String) param.get("page"));
      int limit=StringUtils.isEmpty(param.get("limit"))?1:Integer.parseInt((String) param.get("limit"));
             String depcode   =(String)param.get("depcode");
      // 签名校验
      String hoscode = (String)param.get("hoscode");
      String  hospSign   = (String)param.get("sign");

      String signKey = hospitalSetService.getSignKey(hoscode);

      String encrypt = MD5.encrypt(signKey);

      if(!hospSign.equals(encrypt)){
          throw new YyghException(ResultCodeEnum.SIGN_ERROR);
      }

      ScheduleOrderVo scheduleOrderVo=new ScheduleOrderVo();

       scheduleOrderVo.setHoscode(hoscode);
      scheduleOrderVo.setDepcode(depcode);
       //调用service方法
      Page<Schedule> scheduleList  =scheduleService.findPageScheduleOrder(page,limit,scheduleOrderVo);



      return  Result.ok(scheduleList);

  }

// 删除排班
    @PostMapping("schedule/remove")
    public  Result  remove(HttpServletRequest request){


        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> param = HttpRequestHelper.switchMap(requestMap);

         String hoscode =(String) param.get("hoscode");
             String  hosScheduleId   = (String) param.get("hosScheduleId");

        // 签名校验

        String  hospSign   = (String)param.get("sign");

        String signKey = hospitalSetService.getSignKey(hoscode);

        String encrypt = MD5.encrypt(signKey);

        if(!hospSign.equals(encrypt)){
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }


        scheduleService.remove(hoscode,hosScheduleId);

        return  Result.ok();
    }


}
